package com.qiusuo0226.core.controller;

import com.qiusuo0226.http.util.HttpServletRequestTool;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.core.pojo.query.LoginBean;
import com.qiusuo0226.core.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 登录前端控制层
 * @author qiusuo
 * @date 2023/6/19 15:41
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/interface/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * @return
     */
    @PostMapping("/sign_in")
    public JsonResultModel login(HttpServletRequest request,
                                 @RequestBody @Valid LoginBean bean) {
        if (null != bean) {
            try {
                // 解密入参属性
                bean.decrypt();
            } catch (Exception e) {
                log.error("", e);
                return JsonResultModel.paramsError("参数解密异常");
            }
            // 获取当前请求的IP
            String remoteHost = HttpServletRequestTool.getRemoteHost(request);
            // 登录结果
            return loginService.signIn(remoteHost, bean);
        }
        return JsonResultModel.requestError();
    }



    /**
     * 注册
     * @return
     */
    @PostMapping("/sign_up")
    public JsonResultModel register() {
        return JsonResultModel.requestError();
    }
}

